package wicket.example.components.table.test.page;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxEventBehavior;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import wicket.example.beans.PatientBean;
import wicket.example.components.table.test.base.ExpandableDataTable;
import wicket.example.components.table.test.columns.DataTableColumn;
import wicket.example.providers.PatientBeanDataProvider;

public class ExpandableDataTablePage extends WebPage
{

   private static final long serialVersionUID = 1L;

   public ExpandableDataTablePage()
   {
      List<DataTableColumn<PatientBean>> columns = new ArrayList<DataTableColumn<PatientBean>>();

      columns.add(new DataTableColumn<PatientBean>(new Model<String>("Name"), "name")
      {

         private static final long serialVersionUID = 1L;

         @Override
         public Component getComponent(String componentId, IModel<PatientBean> model)
         {
            return new Label(componentId, model.getObject().getName());
         }

      });

      columns.add(new DataTableColumn<PatientBean>(new Model<String>("Surname"), "surname")
      {

         private static final long serialVersionUID = 1L;

         @Override
         public Component getComponent(String componentId, IModel<PatientBean> model)
         {
            return new Label(componentId, model.getObject().getSurname());
         }

      });

      columns.add(new DataTableColumn<PatientBean>(new Model<String>("Age"))
      {

         private static final long serialVersionUID = 1L;

         @Override
         public Component getComponent(String componentId, IModel<PatientBean> model)
         {
            Label component = new Label(componentId, model.getObject().getAge());
            component.add(new AjaxEventBehavior("onclick")
            {

               private static final long serialVersionUID = 1L;

               @Override
               protected void onEvent(AjaxRequestTarget target)
               {
                  target.appendJavaScript("alert('Click!');");
               }
            });
            return component;
         }

      });
      PatientBeanDataProvider dataProvider = new PatientBeanDataProvider();

      ExpandableDataTable<PatientBean> table = new ExpandableDataTable<PatientBean>("table", columns, dataProvider)
      {

         private static final long serialVersionUID = 1L;

         @Override
         public Component getExpandableContent(String componentID, PatientBean modelObject)
         {
            return new Label(componentID, "Expandable Content: " + modelObject.getName());
         }

      };
      this.add(table);

   }

}
